package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.DrawingMapper;
import com.eastfair.venueservice.dto.DrawingDTO;
import com.eastfair.venueservice.entity.Drawing;
import com.eastfair.venueservice.service.DrawingService;
import com.eastfair.venueservice.service.ExhibitionDrawingService;
import com.eastfair.venueservice.vo.DrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 综合图纸分类
 * </p>
 *
 * @author dq
 * @date 2022-10-27
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class DrawingServiceImpl extends SuperServiceImpl<DrawingMapper, Drawing> implements DrawingService {

    @Resource
    private DrawingMapper drawingMapper;

    @Resource
    private ExhibitionDrawingService exhibitionDrawingService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<Drawing> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(Drawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<DrawingVO> listDrawingVO(DrawingDTO drawingDTO) {
        log.info("listDrawingVO - 查询图纸列表VO, params={}", JSONObject.toJSON(drawingDTO));
        QueryWrapper<Drawing> queryWrapper = createDrawingVOQueryWrapper(drawingDTO);
        return drawingMapper.listDrawingVO(queryWrapper);
    }

    @Override
    public List<Drawing> listDrawing(DrawingDTO drawingDTO) {
        log.info("listDrawing - 查询图纸列表, drawingDTO={}", drawingDTO);
        QueryWrapper<Drawing> queryWrapper = createDrawingQueryWrapper(drawingDTO);
        return list(queryWrapper);
    }

    @Override
    public DrawingVO getDrawingVOById(Long id) {
        log.info("getDrawingVOById - id获取图纸详情, id={}", id);
        Drawing drawing = getById(id);
        if (drawing == null) {
            return null;
        }
        return BeanUtil.toBean(drawing, DrawingVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDrawing(DrawingDTO drawingSaveDTO) {
        log.info("saveDrawing - 保存图纸, drawingSaveDTO={}", drawingSaveDTO);
        Drawing drawing = BeanUtil.toBean(drawingSaveDTO, Drawing.class);
        return save(drawing);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDrawing(DrawingDTO drawingUpdateDTO) {
        log.info("updateDrawing - 修改图纸信息, drawingUpdateDTO={}", drawingUpdateDTO);
        Drawing drawing = BeanUtil.toBean(drawingUpdateDTO, Drawing.class);
        return updateById(drawing);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delDrawing(Long id) {
        log.info("delDrawing - 删除图纸, id={}", id);
        // 删除图纸
        Drawing drawing = Drawing
                .builder()
                .id(id)
                .isDeleted(BusinessConstant.DELETE_YES)
                .build();
        this.updateById(drawing);
        return exhibitionDrawingService.removeByDrawingId(id);
    }

    private QueryWrapper<Drawing> createDrawingQueryWrapper(DrawingDTO drawingDTO) {
        QueryWrapper<Drawing> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Drawing::getIsDeleted, BusinessConstant.DELETE_NO)
                .like(StrUtil.isNotBlank(drawingDTO.getDrawingName()), Drawing::getDrawingName, drawingDTO.getDrawingName())
                .eq(StrUtil.isNotBlank(drawingDTO.getDrawingType()), Drawing::getDrawingType, drawingDTO.getDrawingType())
                .eq(drawingDTO.getIsEnabled() != null, Drawing::getIsEnabled, drawingDTO.getIsEnabled())
                .orderByAsc(Drawing::getSort);
        return queryWrapper;
    }

    private QueryWrapper<Drawing> createDrawingVOQueryWrapper(DrawingDTO drawingDTO) {
        QueryWrapper<Drawing> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("d.is_deleted", BusinessConstant.DELETE_NO)
                .eq(drawingDTO.getExhibitionId() != null, "ed.exhibition_id", drawingDTO.getExhibitionId())
                .eq(StrUtil.isNotBlank(drawingDTO.getSpaceCode()), "ed.space_code", drawingDTO.getSpaceCode())
                .like(StrUtil.isNotBlank(drawingDTO.getDrawingName()), "d.drawing_name", drawingDTO.getDrawingName())
                .eq(StrUtil.isNotBlank(drawingDTO.getDrawingType()), "d.drawing_type", drawingDTO.getDrawingType())
                .eq(drawingDTO.getIsEnabled() != null, "d.is_enabled", drawingDTO.getIsEnabled())
                .groupBy("d.id")
                .orderByAsc("d.sort");
        return queryWrapper;
    }
}
